﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body class="easyui-layout">
  
    <link href="../hcjsui/jquery-easyui-1.3.3/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../hcjsui/jquery-easyui-1.3.3/themes/icon.css" rel="stylesheet" type="text/css" />

    
    <script src="../hcjsui/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="../hcjsui/jquery-easyui-1.3.3/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../hcjsui/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js" charset="utf-8" type="text/javascript"></script>
    <script src="../hcjsui/Scripts/jquery.form.js" type="text/javascript"></script>
    <!--<script src="../hcjsui/js/json2.js" type="text/javascript"></script>-->
    <script src="../hcjsui/js/_Define.js" type="text/javascript"></script>
   <!-- <script src="../hcjsui/js/customer.js" type="text/javascript"></script>-->
   <!-- <script type="text/javascript" src="../hcjsui/js/usbocx.js"></script>-->
    <script src="../hcjsui/Scripts/jquery.validate.js" type="text/javascript"></script>
    <script src="../hcjsui/js/easyui-extend/form.js" type="text/javascript"></script>
<!--    <script src="../hcjsui/js/easyui-extend/window.js" type="text/javascript"></script>
    <script src="../hcjsui/js/validateExtends.js" type="text/javascript"></script>-->
    <script type="text/javascript" src="../js/hc.js"></script>
    <div id="ui_department_layout" class="easyui-layout" data-options="fit:true,border:false">
        <div region="north" title="权限管理" split="false" border="false" style="height: 60px;">
            <table width="98%">
                <tr>
                    <td class="auto-style1">
                        <form id="searchForm" method="post" runat="server">
                            <div>
                                类型：
                                <select id="nofindtype" name="nofindtype" class="easyui-combobox" style="width: 150px;">
                                    <option value="机构" selected="selected">机构</option>
                                    <option value="银行">银行</option>
                                </select>
                                单位名称：<input id="findBranchID" name="findBranchID" />
                                部门：
                             <input class="easyui-combotree" id="findDepID" name="findDepID" />

                                <img style="cursor: pointer; vertical-align: middle;" onclick="$('#findDepID').combotree('clear');"
                                    alt="清空部门" title="清空部门" src="../img/edit-clear.png" />
                                名称：<input id="findEmpName" name="findEmpName" />
                                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="javascript:Search();">查询</a>&nbsp;&nbsp;<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="javascript:Reset();">重置</a>
                            </div>
                        </form>
                    </td>

                </tr>
            </table>
        </div>
        <div region="center" title="对象列表" style="padding: 0px;" border="false">
            <div id="divtablist" class="easyui-tabs" style="padding: 0px;">
                <div title="角色列表" style="padding: 0px;">
                    <table id="grid2"></table>
                </div>
            <!--    <div title="用户列表">
                    <table id="grid1"></table>
                </div>-->

            </div>

        </div>
        <div data-options="region:'east',split:true,border:true,collapsed:false" title="权限结构树"
            style="width: 680px;">
            <input type="hidden" id="objid" name="objid" />
            <input type="hidden" id="objtype" name="objtype" value="user" />
            <input type="hidden" id="accesstype" name="accesstype" value="menu" />
            <table id="ui_obj_access_tree">
            </table>
        </div>
    </div>
    <script type="text/javascript">
        spsetBase("mrBaseInf", "EmpID", "View_User as T", "T.EmpID,T.LoginID,T.EmpName,T.BranchName,T.DepName,T.OfficeTel", "#grid1", "mrobjAccess");
        var Mbranchobj;//机构对象
        var Mbankobj
        var MButtonmap = new HashMap();
        $(function () {
            $('#nofindtype').combobox({
                onChange: function (newValue, oldValue) {
                    if (newValue == "机构") {
                        $("#findBranchID").combobox("loadData", Mbranchobj);
                    }
                    else {
                        $("#findBranchID").combobox("loadData", Mbankobj);
                    }
                }
            });
            hcdosel("BranchID,BranchName", "mrBranch", "type in('本部','分部')", "", "", "BranchID", function (data) {
                Mbranchobj = $.parseJSON(data);
                $("#findBranchID").combobox("loadData", Mbranchobj);
            });
            hcdosel("BranchID,BranchName", "mrBranch", "type in('银行','分行','一级支行')", "", "", "BranchID", function (data) {
                Mbankobj = $.parseJSON(data);
            });
            var MallButton;
            hcdosel("BtnID,BtnText,TabMenuId", "sysButton", "", "", "", "TabMenuId ,BtnNo", function (data) {
                MallButton = $.parseJSON(data);
                for (var item in MallButton) {
                    var jValue = MallButton[item];//key所对应的value
                    var info = "<input type='checkbox'     id='checkbt_" + jValue.BtnID + "' />" + jValue.BtnText + "  ";
                    MButtonmap.putToEnd(jValue.TabMenuId, info);

                }
                BindGrid();
            });
            $('#findBranchID').combobox({
                editable: false,
                valueField: 'BranchID',
                textField: 'BranchName',
                multiple: false,
                onChange: function (newValue, oldValue) {
                    runloadselecttree("#findDepID", newValue);
                },
                onLoadSuccess: function () {
                    var val = $(this).combobox("getData");
                    for (var item in val[0]) {
                        if (item == "BranchID") {
                            $(this).combobox("select", val[0][item]);
                        }
                    }
                }
            });





        });
        var urltree = sposelTreeUrl("mrDepartment as T inner join mrBranch  as T1 on T.BranchID=T1.BranchID ",
            "DepID as ID,DepName  as ShowInfo,T1.BranchName as ShowInfo2,T.ParentId");
        function runloadselecttree(sid, id) {
            $(sid).combotree({
                url: urltree + '&wheresp= and T.BranchID=' + id

            });
        }
        function BindGrid() {
            //$(grid1ID).datagrid({
            //    //pagination: true, //是否显示分页控件
            //    fitColumns: true, //是否自适应宽度
            //    // fit: true, //是否自适应父容器的大小
            //    loadMsg: "努力加载中...", //加载数据时，显示的提示信息
            //    rownumbers: true, //是否显示行号
            //    singleSelect: true, //是否单选
            //    remoteSort: false, //是否启用服务器排序
            //    //sortName: "id", //排序字段
            //    sortOrder: "ASC", //排序类型
            //    striped: true, //行显示交替背景
            //    idField: pTableIdName,
            //    pageSize: 15,
            //    // checkOnSelect :false,
            //    pageList: [10, 15, 20, 30, 40, 50],
            //    columns: [[

            //       { field: "LoginID", title: "账号", width: 100, sortable: true },
            //       { field: "EmpName", title: "姓名", width: 80, sortable: true },
            //       { field: "BranchName", title: "机构", width: 80, sortable: true },
            //       { field: "DepName", title: "部门", width: 80, sortable: true }

            //    ]],

            //    onLoadSuccess: function (data) {
            //        switch (data.message) {
            //            case JsonResult.SessionOut:
            //                $.messager.alert('系统出错', ' 登录超时，请重新登录!');
            //                break;
            //            case JsonResult.Error:
            //                $.messager.alert('系统出错', data.data);
            //                break;
            //        }
            //    },
            //    onBeforeLoad: function (param) {
            //        if (!$(this).datagrid('options').url) {
            //            $(this).datagrid('options').url = spselUrl(); //pageUrl + "?action=sel&tableinfo=" + selTableinfo + "&refields=" + selFieldsinfo + "";
            //        }
            //        $.extend(param, $("#searchForm").formToJson());
            //    },
            //    onClickRow: function (index, row) {
            //        ////  
            //        //departmentIds = "";     //必须先清空全局变量，否则一直累加
            //        //recursionGetIds(row);   //递归获取所有子部门id
            //        //////console.log(departmentIds + row.id);

            //        //if (oldSelectDepartmentId == row.id) {  //点选的是相同的部门就不再请求数据
            //        //    return;
            //        //}
            //        //oldSelectDepartmentId = row.id;   //赋值
            //        var $ui_department_layout = $("#ui_department_layout");
            //        var eastDepartmentUser = $ui_department_layout.layout("panel", "east");

            //        if (eastDepartmentUser.panel("options").collapsed) {   //判断是否展开
            //            $ui_department_layout.layout("expand", "east");
            //        }
            //        eastDepartmentUser.panel("setTitle", row.EmpName + "-人员-权限");
            //        $("#objid").val(row.EmpID);
            //        $("#objtype").val("user");

            //        //初始化勾选
            //        $("input[type='checkbox']").removeAttr("checked");
            //        //加载当前用户权限
            //        $.get(sposelUrl("sysObjAccess as T", "AccessValue,AccessType,DataAccess", " and ObjValue=" + row.EmpID + " and ObjType='user' and AccessType in('menu','button','data')"), //pageUrl + "?action=sel&tableinfo=sysObjAccess as T&refields=AccessValue,AccessType,DataAccess&wheresp= and ObjValue=" + row.EmpID + " and ObjType='user' and AccessType in('menu','button','data')",
            //            function (data) {
            //                var data = JSON.parse(data);
            //                if (data.message == JsonResult.Success) {
            //                    for (var i = 0; i < data.rows.length; i++) {
            //                        var infoobj = data.rows[i];
            //                        if (infoobj["AccessType"] == "menu") $("#check_" + infoobj["AccessValue"]).prop("checked", "true");
            //                        else if (infoobj["AccessType"] == "button") $("#checkbt_" + infoobj["AccessValue"]).prop("checked", "true");
            //                        else if (infoobj["AccessType"] == "data") $("#checkdb_" + infoobj["AccessValue"]).prop("checked", "true");


            //                    }
            //                }
            //            });


            //    }
            //});
            $("#grid2").datagrid({
                fitColumns: true, //是否自适应宽度
                loadMsg: "努力加载中...", //加载数据时，显示的提示信息
                rownumbers: true, //是否显示行号
                singleSelect: true, //是否单选
                remoteSort: false, //是否启用服务器排序
                sortOrder: "ASC", //排序类型
                striped: true, //行显示交替背景
                idField: "",
                columns: [[
                   //{ field: "RoleID", title: "ID", width: 80, sortable: true, checkbox: true },
                   { field: "RoleName", title: "名称", width: 100, sortable: true },
                   { field: "Description", title: "说明", width: 80, sortable: true }
                ]],

                onLoadSuccess: function (data) {
                    switch (data.message) {
                        case JsonResult.SessionOut:
                            $.messager.alert('系统出错', ' 登录超时，请重新登录!');
                            break;
                        case JsonResult.Error:
                            $.messager.alert('系统出错', data.data);
                            break;
                    }
                },
                onBeforeLoad: function (param) {
                    if (!$(this).datagrid('options').url) {
                        $(this).datagrid('options').url = sposelUrl("sysRolesDic as T", "*");//pageUrl + "?action=sel&tableinfo=sysRolesDic as T&refields=*";
                    }

                },
                onClickRow: function (index, row) {

                    var $ui_department_layout = $("#ui_department_layout");
                    var eastDepartmentUser = $ui_department_layout.layout("panel", "east");

                    if (eastDepartmentUser.panel("options").collapsed) {   //判断是否展开
                        $ui_department_layout.layout("expand", "east");
                    }
                    eastDepartmentUser.panel("setTitle", row.RoleName + "-角色-权限");
                    $("#objid").val(row.RoleID);
                    $("#objtype").val("role");

                    //初始化勾选
                    $("input[type='checkbox']").removeAttr("checked");
                    //加载当前角色权限
                    $.get(sposelUrl("sysObjAccess as T", "AccessValue,AccessType,DataAccess", " and ObjValue=" + row.RoleID + " and ObjType='role' and AccessType in('menu','button','data')" + "&timenow=" + new Date().getTime()),
                        //pageUrl + "?action=sel&tableinfo=sysObjAccess as T&refields=AccessValue,AccessType&wheresp= and ObjValue=" + row.RoleID + " and ObjType='role' and AccessType in('menu','button','data')",
                        function (data) {
                            var data = JSON.parse(data);
                            if (data.message == JsonResult.Success) {
                                for (var i = 0; i < data.rows.length; i++) {
                                    var infoobj = data.rows[i];
                                    if (infoobj["AccessType"] == "menu") $("#check_" + infoobj["AccessValue"]).prop("checked", "true");
                                    else if (infoobj["AccessType"] == "button") $("#checkbt_" + infoobj["AccessValue"]).prop("checked", "true");
                                    else if (infoobj["AccessType"] == "data") $("#checkdb_" + infoobj["AccessValue"]).prop("checked", "true");

                                }
                            }
                        });

                }

            });

            var urltree = sposelTreeUrl("View_menu as T",
                " id as ID,c_menuname  as ShowInfo,c_menuname as ShowInfo2,T.ParentId");
            // "../ashx/sp.ashx?action=seltree&tableinfo= View_menu as T &refields= id as ID,c_menuname  as ShowInfo,c_menuname as ShowInfo2,T.[ParentId]";
            $("#ui_obj_access_tree").treegrid({       //初始化treegrid
                url: urltree,
                idField: 'id',
                treeField: 'text',
                // pagination: true, //是否显示分页控件
                // rownumbers: true, //是否显示行号
                fitColumns: true, //是否自适应宽度
                fit: true, //是否自适应父容器的大小
                toolbar:
                    [
                    { id: 'btnAdd', text: '全选', iconCls: 'icon-add', handler: ui_tree_selectall },
        { id: 'btnDel', text: '反选', iconCls: 'icon-delete', handler: ui_tree_selectno },
         { id: 'btnSaveSubmit', text: '保存', iconCls: 'icon-savesubmit', handler: ui_tree_SaveSubmit },
       { id: 'btnopenall', text: '展开', iconCls: 'icon-download', handler: ui_obj_access_tree_expandall },
      { id: 'btnCheckPass', text: '隐藏', iconCls: 'icon-upload', handler: ui_obj_access_tree_collapseall }
                    ],
                columns: [[

                             {
                                 title: '菜单权限', field: 'text', formatter: function (value, row, rowIndex) {
                                     return "<input type='checkbox' onclick=show('" + row.id + "')    id='check_" + row.id + "' />" + row.text;
                                 }, width: 80
                             },
       {
           title: '按钮权限', field: 'xx', formatter: function (value, row, rowIndex) {
               return MButtonmap.get(row.id);
           }
       }, {
           title: '数据权限', field: 'yy', formatter: function (value, row, rowIndex) {
               if (row.ParentId == "0") return "";
               else {
                   return "<input type='checkbox'     onclick=checksondb('" + row.id + "','1') id='checkdb_" + row.id + "_1' />个人 "
                       + "<input type='checkbox'    onclick=checksondb('" + row.id + "','2')  id='checkdb_" + row.id + "_2' />部门 "
                     + "<input type='checkbox'     onclick=checksondb('" + row.id + "','3') id='checkdb_" + row.id + "_3' />本机构 "
                     + "<input type='checkbox'    onclick=checksondb('" + row.id + "','4')  id='checkdb_" + row.id + "_4' />含子机构 "
                     + "<input type='checkbox'    onclick=checksondb('" + row.id + "','5')   id='checkdb_" + row.id + "_5' />不限";
               }

           }
       },
                ]]

            });






        }

        function ui_obj_access_tree_expandall() {
            $("#ui_obj_access_tree").treegrid("expandAll");
        }
        function ui_obj_access_tree_collapseall() {
            $("#ui_obj_access_tree").treegrid("collapseAll");
        }
        function ui_tree_selectall() {
            //alert(1);
            $("input[type='checkbox']").each(function () {
                var id = $(this).attr("id");
                // alert(id);
                if (id.indexOf('check_type') == -1 && (id.indexOf("check_") > -1 || id.indexOf("checkbt_") > -1 || id.indexOf("checkdb_") > -1))
                    //$(this).attr("checked", "true");//第二次之后，不可以！！！
                    //this.checked = true;可以
                    $(this).prop("checked", "true");

            })
        }
        function ui_tree_selectno() {
            $("input[type='checkbox']").each(function () {
                var id = $(this).attr("id");

                if (id.indexOf('check_type') == -1 && (id.indexOf("check_") > -1 || id.indexOf("checkbt_") > -1 || id.indexOf("checkdb_") > -1)) {
                    if (this.checked)
                        $(this).removeAttr("checked");
                    else
                        $(this).prop("checked", "true");
                }

            })
        }
        function Search() {
            $(grid1).datagrid("load");
        }

        function Reset() {
            $("#findRoleName").val("");
        }
        function checksondb(id, num) {
            var s = '#checkdb_' + id + '_' + num;
            if ($(s)[0].checked) {
                for (i = 1; i < num; i++) {
                    $(('#checkdb_' + id + '_' + i))[0].checked = $(s)[0].checked;
                }
            }
            else {
                for (i = 5; i > num; i--) {
                    $(('#checkdb_' + id + '_' + i))[0].checked = $(s)[0].checked;
                }
            }

        }
        function show(checkid) {
            var s = '#check_' + checkid;
            //alert($(s).attr("id"));
            //alert($(s)[0].checked);
            /*选子节点*/
            var nodes = $("#ui_obj_access_tree").treegrid("getChildren", checkid);
            for (i = 0; i < nodes.length; i++) {
                $(('#check_' + nodes[i].id))[0].checked = $(s)[0].checked;

            }
            //选上级节点
            //if(!$(s)[0].checked){
            //    var parent = $("#ui_obj_access_tree").treegrid("getParent", checkid);
            //    $(('#check_'+parent.id))[0].checked  = false;
            //    while(parent){
            //        parent = $("#ui_obj_access_tree").treegrid("getParent", parent.id);
            //        $(('#check_'+parent.id))[0].checked  = false;
            //    }
            //}else{
            //    var parent = $("#ui_obj_access_tree").treegrid("getParent", checkid);
            //    var flag= true;
            //    var sons = parent.sondata.split(',');         
            //    for(j=0;j<sons.length;j++){
            //        if(!$(('#check_'+sons[j]))[0].checked){
            //            flag = false;
            //            break;
            //        }
            //    }
            //    if(flag)
            //        $(('#check_'+parent.id))[0].checked  = true;
            //    while(flag){
            //        parent = $("#ui_obj_access_tree").treegrid("getParent", parent.id);
            //        if(parent){
            //            sons = parent.sondata.split(',');
            //            for(j=0;j<sons.length;j++){
            //                if(!$(('#check_'+sons[j]))[0].checked){
            //                    flag = false;
            //                    break;
            //                }
            //            }
            //        }
            //        if (flag && parent)
            //            $(('#check_'+parent.id))[0].checked  = true;
            //    }
            //}
        }

        function formatcheckbox(val, row) {

            return "<input type='checkbox' onclick=show('" + row.id + "')    id='check_" + row.id + "' " + (row.checked ? 'checked' : '') + "/>" + row.text;
        }
        function init() {
            //去掉结点前面的文件及文件夹小图标
            $(".tree-icon,.tree-file").removeClass("tree-icon tree-file");
            $(".tree-icon,.tree-folder").removeClass("tree-icon tree-folder tree-folder-open tree-folder-closed");
        }
        //获取选中的结点
        function getSelected() {
            var idList = "";
            $("input:checked").each(function () {
                var id = $(this).attr("id");

                if (id.indexOf('check_type') == -1 && id.indexOf("check_") > -1)
                    idList += id.replace("check_", '') + ',';

            })
        }
        function ui_tree_SaveSubmit() {
            var dbList = "";
            var btList = "";
            var idList = "";
            $("input:checked").each(function () {
                var id = $(this).attr("id");

                if (id.indexOf('check_type') == -1 && id.indexOf("check_") > -1)
                    idList += id.replace("check_", '') + ',';
                else if (id.indexOf('check_type') == -1 && id.indexOf("checkbt_") > -1)
                    btList += id.replace("checkbt_", '') + ',';
                else if (id.indexOf('check_type') == -1 && id.indexOf("checkdb_") > -1)
                    dbList += id.replace("checkdb_", '') + ',';

            });
            if ($("#objid").val().length < 1) {
                $.messager.alert("提示信息", "请选择需要设置的对象！");
                return;
            }
            if (idList.length > 1) {
                idList = idList.substring(0, idList.length - 1);
            }
            if (btList.length > 1) {
                btList = btList.substring(0, btList.length - 1);
            }


            $.ajax({
                type: "get",
                url: spPageUrl,
                data: {
                    "action": "doProc",
                    "procName": "doProcSaveUserAcess",
                    "hcpobjvalue": $("#objid").val(),
                    "hcpaccesstype": $("#accesstype").val(),
                    "hcpobjtype": $("#objtype").val(),
                    "hcpaccessvalue": idList,
                    "hcpbtvalue": btList,
                    "hcpdbvalue": dbList
                },

                timeout: 30,
                async: false,
                cache: false,
                error: function () {
                    alert("error");
                },
                success: function (data) {
                    var result = JSON.parse(data);
                    switch (result.message) {
                        case JsonResult.Success:
                            $.messager.show({
                                title: '提示信息',
                                msg: result.data,
                                showType: 'show'
                            });
                            break;
                        case JsonResult.Error:
                            alert(result.data);
                            break;
                        default:
                            break;
                    }

                }
            });
        }






        function HashMap() {
            //定义长度
            var length = 0;
            //创建一个对象
            var obj = new Object();

            /**
            * 判断Map是否为空
            */
            this.isEmpty = function () {
                return length == 0;
            };

            /**
            * 判断对象中是否包含给定Key
            */
            this.containsKey = function (key) {
                return (key in obj);
            };

            /**
            * 判断对象中是否包含给定的Value
            */
            this.containsValue = function (value) {
                for (var key in obj) {
                    if (obj[key] == value) {
                        return true;
                    }
                }
                return false;
            };

            /**
            *向map中添加数据
            */
            this.put = function (key, value) {
                if (!this.containsKey(key)) {
                    length++;
                }
                obj[key] = value;
            };
            this.putToEnd = function (key, value) {
                if (!this.containsKey(key)) {
                    length++;
                    obj[key] = value;
                }
                else { obj[key] = obj[key] + value; }

            };
            /**
            * 根据给定的Key获得Value
            */
            this.get = function (key) {
                return this.containsKey(key) ? obj[key] : "";
            };

            /**
            * 根据给定的Key删除一个值
            */
            this.remove = function (key) {
                if (this.containsKey(key) && (delete obj[key])) {
                    length--;
                }
            };

            /**
            * 获得Map中的所有Value
            */
            this.values = function () {
                var _values = new Array();
                for (var key in obj) {
                    _values.push(obj[key]);
                }
                return _values;
            };

            /**
            * 获得Map中的所有Key
            */
            this.keySet = function () {
                var _keys = new Array();
                for (var key in obj) {
                    _keys.push(key);
                }
                return _keys;
            };

            /**
            * 获得Map的长度
            */
            this.size = function () {
                return length;
            };

            /**
            * 清空Map
            */
            this.clear = function () {
                length = 0;
                obj = new Object();
            };
        }
    </script>
</body>
</html>
